#!/usr/local/bin/python
# -*- coding: utf-8 -*-
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog, QAbstractItemView, QTableWidgetItem
from xToolkit import xstring

import initBalanceSheetPage
from data_alchemy.account_book_alchemy import AccountBookAlchemy
from ui.BalanceSheetDetailDialogUI import Ui_BalanceSheetDetailDialog
import toolkit

class BalanceSheetDetailDialog(QDialog, Ui_BalanceSheetDetailDialog):


    def __init__(self, parent, selectRow, selectText, year, month):
        QDialog.__init__(self, parent)  # 必须此调此构造方法来设置父窗口
        #self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
        self.setupUi(self)

        # 单独移除帮助按钮
        self.setWindowFlag(Qt.WindowContextHelpButtonHint, False)

        self.initDetailTableUI()
        self.initDetailTableData(selectRow, selectText, year, month)




    def initDetailTableUI(self):
        '''
        函数功能：对表格UI进行初始化
        :param selectRow:
        :param selectText:
        :return:
        '''

        #self.setWindowFlags(QtCore.Qt.WindowContextHelpButtonHint)

        self.balance_sheet_detail_TableWidget.setRowCount(initBalanceSheetPage.BALANCE_SHEET_DETAIL_ROW_COUNT)  # 设置行数目
        self.balance_sheet_detail_TableWidget.setColumnCount(initBalanceSheetPage.BALANCE_SHEET_DETAIL_COLUMN_COUNT)  # 设置列数目
        self.balance_sheet_detail_TableWidget.setHorizontalHeaderLabels(initBalanceSheetPage.BALANCE_SHEET_DETAIL_COLUMN_CONTEXT)

        self.balance_sheet_detail_TableWidget.resizeRowsToContents()  # 设置行宽自适应
        self.balance_sheet_detail_TableWidget.resizeColumnsToContents()  # 设置列宽自适应
        self.balance_sheet_detail_TableWidget.setShowGrid(True)  # 设置显示网络线
        self.balance_sheet_detail_TableWidget.setAlternatingRowColors(True)  # 设置表格颜色交错显示
        self.balance_sheet_detail_TableWidget.verticalHeader().setVisible(False)  # 隐藏垂直标题
        self.balance_sheet_detail_TableWidget.horizontalHeader().setStretchLastSection(True)  # 设置最后一列自动填充容

        self.balance_sheet_detail_TableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)  # 设置选中整行
        #self.balance_sheet_detail_TableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)  # 禁止编辑单元格

    def initDetailTableData(self, selectRow, selectText, year, month):
        '''
        函数功能：对话框表格初始化
        :param row:
        :param tableText:
        :return:
        '''
        if selectRow<0 or selectText=="":
            return False


        accountBookAlchemy = AccountBookAlchemy()
        result = accountBookAlchemy.SearchAccountBookByMonthSub(year, month, selectText)
        self.balance_sheet_detail_TableWidget.setRowCount(len(result))  # 设置行数目



        for i in range(len(result)):
            dataRow = result[i]


            categoryName = "%s\t" % (dataRow.subject_name)
            catrgeoyWidgetItem = QTableWidgetItem(categoryName)
            self.balance_sheet_detail_TableWidget.setItem(i, 0, catrgeoyWidgetItem)


            startValueStr = xstring.dispose(toolkit.processValueSignificant(dataRow.start_value)).humanized_amount(complex=True)
            startValueWidgetItem = QTableWidgetItem(startValueStr)
            self.balance_sheet_detail_TableWidget.setItem(i, 1, startValueWidgetItem)


            endValueStr = xstring.dispose(toolkit.processValueSignificant(dataRow.end_value)).humanized_amount(complex=True)
            endValueWidgetItem = QTableWidgetItem(endValueStr)
            self.balance_sheet_detail_TableWidget.setItem(i, 2, endValueWidgetItem)

            differenceValue = dataRow.end_value - dataRow.start_value
            differenceValueStr = xstring.dispose(toolkit.processValueSignificant(differenceValue)).humanized_amount(complex=True)
            differenceValueWidgetItem = QTableWidgetItem(differenceValueStr)
            self.balance_sheet_detail_TableWidget.setItem(i, 3, differenceValueWidgetItem)

            self.balance_sheet_detail_TableWidget.resizeRowsToContents()
            self.balance_sheet_detail_TableWidget.resizeColumnsToContents()